package com.testing.control;

import com.testing.httpclient.HttpClientControl;
import com.testing.utils.ExcelReader;
import com.testing.utils.ExcelWriter;
import com.testing.utils.LoggerConfig;
import com.testing.utils.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lzp
 */
public class HttpExcelControl {

    public HttpExcelControl(){

    }

    public static void main(String[] args) {
        String body = "";
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HH-mm-ss");
        String dates  = simpleDateFormat.format(date);
        String workerDir = System.getProperty("user.dir");
        String filePath = workerDir + "\\case\\InterfaceCases.xlsx";
        ExcelReader excelReader = new ExcelReader(filePath);
        ExcelWriter excelWriter = new ExcelWriter("case/login.xlsx","case/InterfaceCases" + dates + ".xlsx");
        HttpClientControl httpClientControl = new HttpClientControl(excelWriter);
        int number = excelReader.getTotalSheetNo();
        for (int i = 0; i < number; i++) {
            LoggerConfig.LOGGER.info("当前sheet的名字：" + excelReader.getSheetName(i));
            // 指定使用的sheet页
            excelReader.useSheetByIndex(i);
            List<String> listCase = new ArrayList<String>();
            for (int j = 0; j < excelReader.rows; j++){
                // 给出指定的行
                httpClientControl.nowRow = j;
                List<String> line = excelReader.readNextLine();
                if (Utils.optimization(line.get(0)).length() != 0 || Utils.optimization(line.get(1)).length() != 0){
                    LoggerConfig.LOGGER.info("当前行的内容不是测试用例");
                }else {
                    try {
                        switch (Utils.optimization(line.get(4))){
                            case "doPostForm":
                                body = httpClientControl.doPostForm(Utils.optimization(line.get(5)),Utils.optimization(line.get(6)));
                                break;
                            case "doGet":
                                body = httpClientControl.doGet(Utils.optimization(line.get(5)),Utils.optimization(line.get(6)));
                                break;
                            case "saveCookie":
                                httpClientControl.saveCookie();
                                break;
                            case "postJson":
                                body = httpClientControl.doPostJson(Utils.optimization(line.get(5)),Utils.optimization(line.get(6)));
                                break;
                            case "saveParameter":
                                LoggerConfig.LOGGER.info(line.get(2).toString());
                                httpClientControl.saveParameter(Utils.optimization(line.get(5)),Utils.optimization(line.get(6)));
                                break;
                            case "addHeaderPar":
                                httpClientControl.addHeaderPar(Utils.optimization(line.get(6)));
                                break;
                            case "getData":
                                httpClientControl.getData(Utils.optimization(line.get(6)));
                                break;
                            case "addParameterForJsonText":
                                httpClientControl.addParameterForJsonText(Utils.optimization(line.get(6)));
                                break;
                            case "getLoginEncryption":
                                httpClientControl.getLoginEncryption(Utils.optimization(line.get(5)),Utils.optimization(line.get(6)));
                                break;
                            default:
                                LoggerConfig.LOGGER.error("没有找到对应的方法");
                        }
                        switch (Utils.optimization(line.get(6))){
                            case "equals":
                                boolean equalsResult = httpClientControl.equal(Utils.optimization(line.get(8)),Utils.optimization(line.get(9)));
                                LoggerConfig.LOGGER.info("测试结果：" + String.valueOf(equalsResult));
                                break;
                            case "contains":
                                boolean containsResult = httpClientControl.contains(Utils.optimization(line.get(9)));
                                LoggerConfig.LOGGER.info("测试结果：" + String.valueOf(containsResult));
                                break;
                            default:
                                LoggerConfig.LOGGER.error("没有找到对应的方法");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        excelReader.close();
        excelWriter.save();
    }
}
